package com.laex;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.DateField;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;

import com.laex.model.Expense;

public class ExpenseManager extends Form {

	private Command addExpense;
	private Command selectCategory;
	private DateField date;
	private TextField amount;

	public ExpenseManager(Command back) {
		super("Expense Manager");

		date = new DateField("Date", DateField.DATE);
		amount = new TextField("Amt", "", 100, TextField.NUMERIC);

		addExpense = new Command("Add", Command.ITEM, 1);
		selectCategory = new Command("Category", Command.ITEM, 2);

		this.append(date);
		this.append(amount);

		this.addCommand(addExpense);
		this.addCommand(selectCategory);
		this.addCommand(back);
	}

	public Command getAddCommand() {
		return addExpense;
	}

	public Command getSelectCatCommand() {
		return selectCategory;
	}

	public void addExpense(String cat) {
		try {
			String dateStr = date.getDate().toString().trim();
			String amtStr = amount.getString().trim();
			if (dateStr.length() != 0 && amtStr.length() != 0
					&& cat.length() != 0) {
				
				Expense exp = new Expense();
				exp.setDate(dateStr);
				exp.setAmount(amtStr);
				exp.setCategory(cat);
				
				RecordUtil.insertExpense(exp);
				date.setDate(null);
				amount.setString("");
			}
		} catch (NullPointerException ne) {
			
		}
	}

}
